#! /bin/bash
export LANG=en_US.UTF-8

CURNUM=				#当前的性能参数

DataResultStr=		#性能参数字符串

FLAGE=
FLAGEID=
#是否输出FLAGPID
FLAGEOPID=1
#是否输出RUNTIME
RUNTIME=1
#是否输出STARTTIME
STARTTIME=0
function gotErr(){
	if [ "$1" -eq 0 ];then
		echo "mailstatedes=各项指标恢复正常 statedes=各项指标恢复正常|$DataResultStr"
	elif [ "$1" -eq 3 ];then 
		if [ "$FLAGEOPID" -eq 1 ];then
			CURNUM=$FLAGEID
			parseFileds "FLAGEPID" $CURNUM $CURNUM
		fi
		echo "mailstatedes=无法获取到指标,疑似组件故障,请确认并请检查监控脚本和运维平台配置 statedes=无法获取到指标,疑似组件故障,请确认并请检查监控脚本和运维平台配置|$DataResultStr"
	fi

	exit $1
}

#返回是否服务是否存活
function isAlive(){
	FF=$(echo "$FLAGE"|awk -F ',' '{print $1}')
	PP=$(echo "$FLAGE"|awk -F ',' '{print $2}')
	isAliveStr=$(ps aux | grep "$FF" | grep "$PP" | grep -v grep | grep -v check_programAlive.sh| wc -l)

	if [[ "$isAliveStr" -lt 1 ]];then
		gotErr 3
	fi
	FLAGEID=$(ps aux | grep "$FF" | grep "$PP" | grep -v grep | grep -v check_programAlive.sh | awk '{print $2}')
}


#处理连接数
function getConnNum(){
	CURNUM=$(netstat -apn | grep $FLAGEID| grep ESTABLISHED  | wc -l)
}

#ps aux 4是内存 累加4的值
function getMemory(){
	CURNUM=$(ps aux | grep $FLAGEID |awk 'BEGIN{sum=0}{sum+=$6}END{print sum}')
}

#ps aux 3是CPU 累加3的值
function getCPU(){
	CURNUM=$(ps aux | grep $FLAGEID |awk 'BEGIN{sum=0}{sum+=$3}END{print sum}')
}

#ps elm 是PID所包含的线程数目
function getThreadNum(){
	CURNUM=$(ps -mp $FLAGEID |wc -l | awk 'BEGIN{sum=0}{sum=$1-2}END{print sum}')
}

#ps -eo pid,etime 是查询进程运行时间
function getRuntime(){
	CURNUM=$(ps -eo pid,etime | grep $FLAGEID |awk '{if($1=='$FLAGEID') print $2}')
}

#ps -eo pid,lstart是查询进程的开始时间
function getStarttime(){
	CURNUM=$(ps -eo pid,lstart | grep $FLAGEID |awk '{if($1=='$FLAGEID') print $2 ,$3 ,$4 ,$5 ,$6}')
}
#依据传入的参数值来判断我们要监控的服务是什么
#然后通过各个函数获取到具体的值
#值之间的
function analysisStat(){
	getConnNum
	parseFileds "ConnectionNum"

	getMemory
	parseFileds "Memory"

	getCPU
	parseFileds "CPU"

	getThreadNum
	parseFileds "Thread"
	
	if [ "$FLAGEOPID" -eq 1 ];then
		CURNUM=$FLAGEID
		parseFileds "FLAGEPID" $CURNUM $CURNUM
	fi
	if [ "$RUNTIME" -eq 1 ];then
			getRuntime
			parseTimeFileds "RUNTIME" $RUNTIME $RUNTIME		
	fi
	if [ "$STARTTIME" -eq 1 ];then
			getStarttime
			parseTimeFileds "STARTTIME" $STARTTIME $STARTTIME		
	fi
	
	
}
function parseTimeFileds(){
	val1=$(echo "$CURNUM $2")
	val2=$(echo "$CURNUM $3")
  DataResultStr="$DataResultStr $1=$CURNUM;$2;$3;0;0"
}

function parseFileds(){
	DataResultStr="$DataResultStr $1=$CURNUM"
}

#分析结果 给出给出状态
function analysisResult(){
	gotErr 0
}

#输出错误信息并且退出程序
function showHelp(){
	if [ "$1" != "" ];then
		echo "请输入$1"
	fi
	echo "check_programAlive.sh 用于监控进程的存活状态，并收集CPU，内存，连接数及线程数 参数如下"
	echo "check_programAlive.sh -f <str>"
	echo "-f <str> 特征字符串 用于标记需要监控的进程"
	exit 1
}


while getopts "f:h" arg
do
	case $arg in
		h)
			showHelp
			;;
		f)
			FLAGE=$OPTARG
			;;
		?) 
			showHelp
			;;
	esac
done

if [[ "$FLAGE" == "" ]];then
	showHelp "特征项"
fi


#插件正题部分 
#确定存活的
#启动
#分析

isAlive
analysisStat
analysisResult